﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using MobySharp;
using MobySharp.Actions;
using MobySharp.Results;

namespace DemoApp
{
    public partial class Form1 : Form
    {
        public Form1(string[] args)
        {
            InitializeComponent();
            if (args.Length > 0)

                try
                {
                    // first param is probably a path to an image
                    pictureBox.ImageLocation = args[0];
                }
                catch
                {
                }
        }

        private void postButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Properties.Settings.Default.Username) ||
                string.IsNullOrEmpty(Properties.Settings.Default.PIN))
            {
                MessageBox.Show("You have not set your Mobypicture username and PIN", "Account settings",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                var s = new Settings();
                if (s.ShowDialog() == DialogResult.Cancel)
                    return;
            }

            // Construct a Mobypicture object
            var moby = new Mobypicture(Properties.Settings.Default.Key);
            
            // Set up a action to post media. Constructor parameters are the properties 
            // that are required by the Mobypicture API
            var action = new PostMedia(Properties.Settings.Default.Username,
                                       Properties.Settings.Default.PIN,
                                       pictureBox.ImageLocation);

            // Fill some non-required properties
            action.Title = titleTextBox.Text;
            action.Description = descriptionTextBox.Text;
            action.Tags.AddRange(groupsListBox.Items.OfType<string>());
            action.Hashtag.AddRange(hashtagsListBox.Items.OfType<string>());
            action.Img = pictureBox.ImageLocation;

            // Execute the action and get the result
            var r = moby.PostMedia(action);

            // Result code 0 is OK, do something meaningful with if (or not)
            if (r.Result.Code == 0)
            {
                var frm = new ResultForm(r.Url);
                frm.ShowDialog();

                ClearAll();
            }
            else
                MessageBox.Show(r.Result.Message, "Post error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void ClearAll()
        {
            titleTextBox.Clear();
            descriptionTextBox.Clear();
            groupTextBox.Clear();
            hashtagTextBox.Clear();
            groupsListBox.Items.Clear();
            hashtagsListBox.Items.Clear();
            pictureBox.Image = null;
        }

        private void settingasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var s = new Settings();
            s.ShowDialog();
        }

        private void hashtagTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Return)
            {
                hashtagsListBox.Items.Add(hashtagTextBox.Text);
                hashtagTextBox.Clear();
            }
        }

        private void groupTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Return)
            {
                groupsListBox.Items.Add(groupTextBox.Text);
                groupTextBox.Clear();
            }
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            // 
            var fd = new OpenFileDialog();
            fd.Multiselect = false;
            fd.Filter =
                "Image Files|*.jpeg;*.jpg;*.png;*.bmp;*.gif|Audio Files|mp3;*.wma;*.aac;*.aif;*.au;*.flac;*.ra;*.wav;*.ogg;*.3gp|Video Files|*.flv;*.mpeg;*.mkv;*.wmv;*.mov;*.3gp;*.mp4;*.avi";

            if (fd.ShowDialog() == DialogResult.OK)
            {
                // OK, won't work for non-image files but you get the point
                pictureBox.ImageLocation = fd.FileName;
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void pictureBox_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])(e.Data.GetData(DataFormats.FileDrop, false));
            
            if(files.Length>0)
            {
                var f = files[0];
                pictureBox.ImageLocation = f;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox.AllowDrop = true;
        }

        private void pictureBox_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void wwwmobypicturecomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://www.mobypicture.com/");
        }
    }
}